package com.song.common.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Date;

public class JwtUtil {

    /**
     * 密钥
     */
    public static final String SECRET_KEY = "FHZZ";

    /**
     * token过期时间
     */
    public static final int EXPIRE_TIME = 24 * 60 * 60 * 365;

    /**
     * 签名
     * @param username
     * @return token令牌
     */
    public synchronized static String sign(String username){
        try {
            //生成时间
            Date createDate = new Date();
            //过期时间
            Date expireDate = DateUtil.offsetSecond(createDate, EXPIRE_TIME);
            //生成算法
            Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
            return JWT.create()
                    .withClaim("username", username)
                    .withIssuedAt(createDate)
                    .withExpiresAt(expireDate)
                    .sign(algorithm);
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 验证token有效期
     * @param token
     * @param username
     * @return 有效：true 过期：false
     */
    public synchronized static boolean verify(String token, String username){
        try {
            Algorithm algorithm = Algorithm.HMAC256(SECRET_KEY);
            JWTVerifier jwtVerifier = JWT.require(algorithm)
                    .withClaim("username", username)
                    .build();
            DecodedJWT decodedJWT = jwtVerifier.verify(token);
            Date now = new Date();
            Date expiresDate = decodedJWT.getExpiresAt();
            if (now.after(expiresDate)){
                return false;
            }
        }catch (Exception e){
            return false;
        }
        return true;
    }

    /**
     * 获取token登录用户名
     * @param token
     * @return
     */
    public static String getUsername(String token) {
        try {
            String username = "";
            if(StrUtil.isNotEmpty(token)){
                DecodedJWT jwt = JWT.decode(token);
                username = jwt.getClaim("username").asString();
            }
            return username;
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 获取token过期时间
     * @param token
     * @return
     */
    public static Date getExpired(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getExpiresAt();
        } catch (JWTDecodeException e) {
            return null;
        }
    }

    /**
     * 获取token生成时间
     * @param token
     * @return
     */
    public static Date getIssued(String token) {
        try {
            DecodedJWT jwt = JWT.decode(token);
            return jwt.getIssuedAt();
        } catch (JWTDecodeException e) {
            return null;
        }
    }
}
